package com.soft.mainsite.module.data.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.soft.mainsite.common.utils.CmsUtil;
import com.soft.mainsite.common.utils.JsonUtil;
import com.soft.mainsite.module.data.dao.AppointmentDao;
import com.soft.mainsite.module.data.dto.AppointmentDto;
import com.soft.mainsite.module.data.dto.AppointmentQueryParams;
import com.soft.mainsite.module.data.service.AppointmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author: hjm
 * @Date: 2020/11/25 0:24
 * Stay hungry,stay foolish!
 */
@Slf4j
@Component
public class AppointmentServiceImpl implements AppointmentService {

    @Resource
    private AppointmentDao appointmentDao;

    /**
     * 只读事务标识，读写分离
     *
     * @param queryParams
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @Transactional(readOnly = true, rollbackFor = Exception.class)
    @Override
    public PageInfo<AppointmentDto> pageAppointmentByConditions(AppointmentQueryParams queryParams, Integer pageNumber, Integer pageSize) {

        PageHelper.startPage(pageNumber, pageSize);

        List<AppointmentDto> appointmentDtoList = appointmentDao.pageAppointmentByConditions(queryParams);

        return new PageInfo<>(appointmentDtoList);
    }

    @Override
    public String delete(Long[] ids) {
        int i = appointmentDao.delete(ids);
        if (i > 0) {
            return JsonUtil.toSuccess("操作成功", "appointment-tab", false);
        }
        return JsonUtil.toError("操作失败");
    }

    @Override
    public String doAppointment(Long[] ids) {
        int i = appointmentDao.doAppointment(ids);
        if (i > 0) {
            return JsonUtil.toSuccess("操作成功", "appointment-tab", false);
        }
        return JsonUtil.toError("操作失败");
    }

    @Override
    public String update(Long[] ids) {
        if (CmsUtil.isNullOrEmpty(ids)){
            return JsonUtil.toError("请选择要逻辑删除的用户资料！");
        }
        int i = appointmentDao.update(ids);
        if (i > 0) {
            return JsonUtil.toSuccess("操作成功", "appointment-tab", false);
        }
        return JsonUtil.toError("操作失败");
    }
}
